package com.fjwt.gz.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjwt.gz.core.constants.CS;
import com.fjwt.gz.db.entity.SysEntitlement;
import com.fjwt.gz.db.entity.SysRoleEntRela;
import com.fjwt.gz.db.entity.SysUser;
import com.fjwt.gz.service.mapper.SysRoleEntRelaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 系统角色权限关联表 服务实现类
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2020-06-13
 */
@Service
public class SysRoleEntRelaService extends ServiceImpl<SysRoleEntRelaMapper, SysRoleEntRela> {

    @Autowired private SysEntitlementService sysEntitlementService;

    /** 根据人查询出所有权限ID集合  */
    public List<String> selectEntIdsByUserId(Long userId, Byte userType, String sysType){

        // 返回结果
        List<String> resultEntIdList = new ArrayList<>();

//        if(userType == SysUser.UEST_TYPE_ADMIN){ // 超管查询所有的数据

//            sysEntitlementService.list(SysEntitlement.gw().select(SysEntitlement::getEntId).eq(SysEntitlement::getSysType, sysType).eq(SysEntitlement::getState, CS.PUB_USABLE)
//            ).stream().forEach(r -> resultEntIdList.add(r.getEntId()));


//        }else{ // 查询用户的角色配置权限
            baseMapper.selectEntIdsByUserId(userId, sysType).stream().forEach( r -> resultEntIdList.add(r));
//        }

        // 数据为空
//        if(resultEntIdList.isEmpty()){
//            return resultEntIdList;
//        }

        return resultEntIdList;
    }


    /** 重置 角色 - 权限 关联关系 **/
    @Transactional
    public void resetRela(String roleId, List<String> entIdList){

        //1. 删除
        this.remove(SysRoleEntRela.gw().eq(SysRoleEntRela::getRoleId, roleId));

        //2. 插入
        for (String entId : entIdList) {
            SysRoleEntRela r = new SysRoleEntRela();
            r.setRoleId(roleId); r.setEntId(entId);
            this.save(r);
        }

    }





}
